﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.MODEL.FoundationModel;
using MINERP.DAL;
using MINERP.BLL.Common;

namespace MINERP.BLL.ComplexOrder
{
    public class ComplexOrderBll
    {
        private WorkFlowListBLL _wflBll;
        public ComplexOrderBll()
        {
            _wflBll = new WorkFlowListBLL();
        }

        public List<ComplexOrderInfo> GetAllComplexOrder(int Techid)
        {
            List<ComplexOrderInfo> ListReturn = new List<ComplexOrderInfo>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {

                List<WorkOrderFlowList> woflList = db.WorkOrderFlowList.Where(w => w.orderInfo.status == 1 && w.Status == 1 && w.tb_Tech.TechId == Techid).ToList();
                foreach (WorkOrderFlowList wofl in woflList)
                {
                    if (!wofl.tb_TechReference.IsLoaded)
                    {
                        wofl.tb_TechReference.Load();
                    }
                    if (!wofl.orderInfoReference.IsLoaded)
                    {
                        wofl.orderInfoReference.Load();
                    }
                    if (!wofl.orderInfo.Tb_ProductReference.IsLoaded)
                    {
                        wofl.orderInfo.Tb_ProductReference.Load();
                    }
                    if (!wofl.orderInfo.Tb_Product.Sub_Product.IsLoaded)
                    {
                        wofl.orderInfo.Tb_Product.Sub_Product.Load();
                    }
                    if (!wofl.orderInfo.Tb_OrderBagReference.IsLoaded)
                    {
                        wofl.orderInfo.Tb_OrderBagReference.Load();

                    }
                    if (!wofl.orderInfo.Tb_OrderBag.ShopReference.IsLoaded)
                    {
                        wofl.orderInfo.Tb_OrderBag.ShopReference.Load();
                    }
                    if (wofl.tb_TechReference.IsLoaded)
                    {
                        wofl.tb_TechReference.Load();
                    }
                    ComplexOrderInfo coi = new ComplexOrderInfo();
                    coi.OrderBagId = wofl.orderInfo.Tb_OrderBag.OrderBagId;
                    coi.OrderTime = wofl.orderInfo.Tb_OrderBag.OrderTime;
                    coi.OrilNum = wofl.orderInfo.OrilNum;
                    coi.OrderProductId = wofl.orderInfo.OrderProductId;
                    coi.OrderProductName = wofl.orderInfo.OrderProductName;
                    coi.ProductNum = wofl.orderInfo.productNum;
                    coi.Priority = (int)wofl.orderInfo.Priority;
                    coi.ShopName = wofl.orderInfo.Tb_OrderBag.Shop.shopName;
                    coi.TechId = wofl.tb_Tech.TechId;
                    coi.TechName = wofl.tb_Tech.TechName;
                    coi.CtmName = wofl.orderInfo.CtmName;
                    coi.SubProductList = new List<ProductStoreInfo>();
                    if (wofl.orderInfo.Tb_Product.Sub_Product.Count > 0)
                    {
                        foreach (Sub_Product sub in wofl.orderInfo.Tb_Product.Sub_Product)
                        {
                            Tb_Product sunProduct = db.Tb_Product.FirstOrDefault(p => p.ProuctId == sub.Containid);
                            sunProduct.Tb_PorductCategoryReference.Load();
                            ProductStoreInfo subinfo = new ProductStoreInfo();

                            subinfo.ClassName = sunProduct.Tb_PorductCategory.ProductCategoryName;
                            subinfo.LockNum = sunProduct.LockNum;
                            subinfo.UnitNum = sub.Num;
                            subinfo.Num = sunProduct.StoreNum;
                            subinfo.ProductName = sunProduct.ProductName + "(" + sunProduct.Width + "x" + sunProduct.Height + ")";
                            subinfo.ProductSize = sunProduct.Width + "x" + sunProduct.Height;
                            coi.SubProductList.Add(subinfo);
                        }
                    }
                    else
                    {
                        wofl.orderInfo.Tb_Product.Tb_PorductCategoryReference.Load();
                        ProductStoreInfo subinfo = new ProductStoreInfo();

                        subinfo.ClassName = wofl.orderInfo.Tb_Product.Tb_PorductCategory.ProductCategoryName;
                        subinfo.LockNum = wofl.orderInfo.Tb_Product.LockNum;
                        subinfo.Num = wofl.orderInfo.Tb_Product.StoreNum;
                        subinfo.ProductName = wofl.orderInfo.Tb_Product.ProductName + "(" + wofl.orderInfo.Tb_Product.Width + "x" + wofl.orderInfo.Tb_Product.Height + ")";
                        subinfo.ProductSize = wofl.orderInfo.Tb_Product.Width + "x" + wofl.orderInfo.Tb_Product.Height;
                        subinfo.UnitNum = 1;
                        coi.SubProductList.Add(subinfo);
                    }
                    ListReturn.Add(coi);
                }

            }
            return ListReturn;

        }

        public bool MakeSureProduct(ComplexOrderInfo CurrProductOrder, int techID, int UserID)
        {
            bool returnBool = false;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                if (CurrProductOrder.ProductNum == CurrProductOrder.OperNum)
                {
                    orderInfo currOrder = db.orderInfo.FirstOrDefault(o => o.OrderProductId == CurrProductOrder.OrderProductId);
                    //讲分配的数量产生冻结数量
                    if (!currOrder.Tb_ProductReference.IsLoaded)
                    {
                        currOrder.Tb_ProductReference.Load();
                    }
                    if (!currOrder.Tb_Product.StoreOperRec.IsLoaded)
                    {
                        currOrder.Tb_Product.StoreOperRec.Load();
                    }
                    //currOrder.Tb_Product.StoreOperRec.Add()
                    //写入操作
                    StoreOperRec sor = new StoreOperRec();
                    sor.OperTime = DateTime.Now;
                    sor.OperRemark = "";
                    sor.StoreOperid = UserID;
                    sor.Tb_Product = currOrder.Tb_Product;
                    sor.users = db.users.FirstOrDefault(u => u.userID == UserID);
                    sor.OperBeforeNum = currOrder.Tb_Product.StoreNum;
                    sor.OperBeforeLockNum = currOrder.Tb_Product.LockNum;
                    currOrder.Tb_Product.LockNum += CurrProductOrder.ProductNum;
                    sor.OperAfterLockNum = currOrder.Tb_Product.LockNum;
                    sor.OperAfterNum = currOrder.Tb_Product.StoreNum;
                    currOrder.Tb_Product.StoreOperRec.Add(sor);
                    db.AddToStoreOperRec(sor);
                    returnBool = _wflBll.OverCurTech(techID, UserID, currOrder, db);
                }
                else
                {
                    orderInfo currOrder = db.orderInfo.FirstOrDefault(o => o.OrderProductId == CurrProductOrder.OrderProductId);
                    if (!currOrder.Tb_OrderBagReference.IsLoaded)
                    {
                        currOrder.Tb_OrderBagReference.Load();
                    }
                    if (!currOrder.Tb_ProductReference.IsLoaded)
                    {
                        currOrder.Tb_ProductReference.Load();
                    }
                    if (!currOrder.WorkOrderFlowList.IsLoaded)
                    {
                        currOrder.WorkOrderFlowList.Load();
                    }
                    if(!currOrder.CommonChargeItem.IsLoaded)
                    {
                        currOrder.CommonChargeItem.Load();
                    }
                    orderInfo newOrder = new orderInfo();
                    newOrder.Tb_Product = currOrder.Tb_Product;
                    newOrder.Tb_OrderBag = currOrder.Tb_OrderBag;
                    newOrder.Area = currOrder.Area;
                    newOrder.CompleteTime = currOrder.CompleteTime;
                    newOrder.ComputerName = currOrder.ComputerName;
                    newOrder.CtmName = currOrder.CtmName;
                    newOrder.Height = currOrder.Height;
                    newOrder.MultiPageNum = 0;
                    newOrder.isCount = 0;
                    newOrder.IsEdit = false;
                    newOrder.MultiPageNum = 0;
                    newOrder.OrderPath = currOrder.OrderPath;
                    newOrder.OrderProductCode = "222222";
                    newOrder.OrderProductName = currOrder.OrderProductName;
                    newOrder.OrderType = currOrder.OrderType;
                    newOrder.OrilNum = currOrder.OrilNum;
                    newOrder.OrilOrderPath = currOrder.OrilOrderPath;
                    newOrder.PageNum = currOrder.PageNum;
                    newOrder.ParentProductId = currOrder.ParentProductId;
                    newOrder.PrePriceCount = currOrder.PrePriceCount;
                    newOrder.Priority = currOrder.Priority;
                    newOrder.productNum = CurrProductOrder.OperNum;//新的数量
                    CurrProductOrder.ProductNum-= CurrProductOrder.OperNum;//新的数量
                    currOrder.productNum -= CurrProductOrder.OperNum;
                    newOrder.ProductType = currOrder.ProductType; ;
                    newOrder.Remark = currOrder.Remark;
                    newOrder.SinglePageNum = 0;
                    newOrder.status = currOrder.status;
                    newOrder.Width = currOrder.Width;
                    //newOrder.WorkOrderFlowList = new System.Data.Objects.DataClasses.EntityCollection<WorkOrderFlowList>();
                    foreach (WorkOrderFlowList oldw in currOrder.WorkOrderFlowList)
                    {
                        oldw.tb_TechReference.Load();
                        WorkOrderFlowList wolf = new WorkOrderFlowList();
                        wolf.AssignTime = oldw.AssignTime;
                        wolf.CompletedTime = oldw.CompletedTime;
                        wolf.IsEdit = oldw.IsEdit;
                        wolf.orderInfo = newOrder;
                        wolf.PreCompleteTime = oldw.PreCompleteTime;
                        wolf.ReceiveTime = oldw.ReceiveTime;
                        wolf.Status = oldw.Status;
                        wolf.tb_Tech = oldw.tb_Tech;
                        wolf.UserId = oldw.UserId;
                        wolf.WorkOrder = oldw.WorkOrder;
                        newOrder.WorkOrderFlowList.Add(wolf);
 
                    }
                    //newOrder.CommonChargeItem = new System.Data.Objects.DataClasses.EntityCollection<CommonChargeItem>();
                    foreach (CommonChargeItem cci in currOrder.CommonChargeItem)
                    {
                        CommonChargeItem ncci = new CommonChargeItem();
                        ncci.AreaPrice = cci.AreaPrice;
                        ncci.CommonChargeItemId = cci.CommonChargeItemId;
                        ncci.ItemName = cci.ItemName;
                        //ncci.orderInfo = newOrder;
                        ncci.SinglePrice = cci.SinglePrice;
                        cci.UnitPrice = cci.UnitPrice;
                        newOrder.CommonChargeItem.Add(ncci);
 
                    }
                    //讲分配的数量产生冻结数量
                    if (!currOrder.Tb_ProductReference.IsLoaded)
                    {
                        currOrder.Tb_ProductReference.Load();
                    }
                    //写入操作
                    StoreOperRec sor = new StoreOperRec();
                    sor.OperTime = DateTime.Now;
                    sor.OperRemark = "";
                    sor.StoreOperid = UserID;
                    sor.Tb_Product = currOrder.Tb_Product;
                    sor.users = db.users.FirstOrDefault(u => u.userID == UserID);
                    sor.OperBeforeNum = currOrder.Tb_Product.StoreNum;
                    sor.OperBeforeLockNum = currOrder.Tb_Product.LockNum;
                    currOrder.Tb_Product.LockNum += CurrProductOrder.OperNum;
                    sor.OperAfterLockNum = currOrder.Tb_Product.LockNum;
                    sor.OperAfterNum = currOrder.Tb_Product.StoreNum;
                    currOrder.Tb_Product.StoreOperRec.Add(sor);
                    db.AddToStoreOperRec(sor);
                   
                    db.SaveChanges();//创建新产品
                    
                    //吧新产品的流程改到下一个节点
                    newOrder.OrderProductCode = "2" + string.Format("{0:d9}", Convert.ToInt32(newOrder.OrderProductId));
                    returnBool = _wflBll.OverCurTech(techID, UserID, newOrder, db);

                }
              
            }
            return returnBool;
        }

        public bool MakeSureProductList(List<ComplexOrderInfo> inProductOrderList, int techID, int UserID)
        {
            bool returnBool = false;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                foreach (ComplexOrderInfo CurrProductOrder in inProductOrderList)
                {
                    if (CurrProductOrder.ProductNum == CurrProductOrder.OperNum)
                    {
                        orderInfo currOrder = db.orderInfo.FirstOrDefault(o => o.OrderProductId == CurrProductOrder.OrderProductId);
                        //讲分配的数量产生冻结数量
                        if (!currOrder.Tb_ProductReference.IsLoaded)
                        {
                            currOrder.Tb_ProductReference.Load();
                        }
                        if (!currOrder.Tb_Product.StoreOperRec.IsLoaded)
                        {
                            currOrder.Tb_Product.StoreOperRec.Load();
                        }
                        //currOrder.Tb_Product.StoreOperRec.Add()
                        //写入操作
                        StoreOperRec sor = new StoreOperRec();
                        sor.OperTime = DateTime.Now;
                        sor.OperRemark = "";
                        sor.StoreOperid = UserID;
                        sor.Tb_Product = currOrder.Tb_Product;
                        sor.users = db.users.FirstOrDefault(u => u.userID == UserID);
                        sor.OperBeforeNum = currOrder.Tb_Product.StoreNum;
                        sor.OperBeforeLockNum = currOrder.Tb_Product.LockNum;
                        currOrder.Tb_Product.LockNum += CurrProductOrder.ProductNum;
                        sor.OperAfterLockNum = currOrder.Tb_Product.LockNum;
                        sor.OperAfterNum = currOrder.Tb_Product.StoreNum;
                        currOrder.Tb_Product.StoreOperRec.Add(sor);
                        db.AddToStoreOperRec(sor);
                        returnBool = _wflBll.OverCurTech(techID, UserID, currOrder, db);
                    }
                    else
                    {
                        orderInfo currOrder = db.orderInfo.FirstOrDefault(o => o.OrderProductId == CurrProductOrder.OrderProductId);
                        if (!currOrder.Tb_OrderBagReference.IsLoaded)
                        {
                            currOrder.Tb_OrderBagReference.Load();
                        }
                        if (!currOrder.Tb_ProductReference.IsLoaded)
                        {
                            currOrder.Tb_ProductReference.Load();
                        }
                        if (!currOrder.WorkOrderFlowList.IsLoaded)
                        {
                            currOrder.WorkOrderFlowList.Load();
                        }
                        if (!currOrder.CommonChargeItem.IsLoaded)
                        {
                            currOrder.CommonChargeItem.Load();
                        }
                        orderInfo newOrder = new orderInfo();
                        newOrder.Tb_Product = currOrder.Tb_Product;
                        newOrder.Tb_OrderBag = currOrder.Tb_OrderBag;
                        newOrder.Area = currOrder.Area;
                        newOrder.CompleteTime = currOrder.CompleteTime;
                        newOrder.ComputerName = currOrder.ComputerName;
                        newOrder.CtmName = currOrder.CtmName;
                        newOrder.Height = currOrder.Height;
                        newOrder.MultiPageNum = 0;
                        newOrder.isCount = 0;
                        newOrder.IsEdit = false;
                        newOrder.MultiPageNum = 0;
                        newOrder.OrderPath = currOrder.OrderPath;
                        newOrder.OrderProductCode = "222222";
                        newOrder.OrderProductName = currOrder.OrderProductName;
                        newOrder.OrderType = currOrder.OrderType;
                        newOrder.OrilNum = currOrder.OrilNum;
                        newOrder.OrilOrderPath = currOrder.OrilOrderPath;
                        newOrder.PageNum = currOrder.PageNum;
                        newOrder.ParentProductId = currOrder.ParentProductId;
                        newOrder.PrePriceCount = currOrder.PrePriceCount;
                        newOrder.Priority = currOrder.Priority;
                        newOrder.productNum = CurrProductOrder.OperNum;//新的数量
                        CurrProductOrder.ProductNum -= CurrProductOrder.OperNum;//新的数量
                        newOrder.ProductType = currOrder.ProductType; ;
                        newOrder.Remark = currOrder.Remark;
                        newOrder.SinglePageNum = 0;
                        newOrder.status = currOrder.status;
                        newOrder.Width = currOrder.Width;
                        //newOrder.WorkOrderFlowList = new System.Data.Objects.DataClasses.EntityCollection<WorkOrderFlowList>();
                        foreach (WorkOrderFlowList oldw in currOrder.WorkOrderFlowList)
                        {
                            oldw.tb_TechReference.Load();
                            WorkOrderFlowList wolf = new WorkOrderFlowList();
                            wolf.AssignTime = oldw.AssignTime;
                            wolf.CompletedTime = oldw.CompletedTime;
                            wolf.IsEdit = oldw.IsEdit;
                            wolf.orderInfo = newOrder;
                            wolf.PreCompleteTime = oldw.PreCompleteTime;
                            wolf.ReceiveTime = oldw.ReceiveTime;
                            wolf.Status = oldw.Status;
                            wolf.tb_Tech = oldw.tb_Tech;
                            wolf.UserId = oldw.UserId;
                            wolf.WorkOrder = oldw.WorkOrder;
                            newOrder.WorkOrderFlowList.Add(wolf);

                        }
                        //newOrder.CommonChargeItem = new System.Data.Objects.DataClasses.EntityCollection<CommonChargeItem>();
                        foreach (CommonChargeItem cci in currOrder.CommonChargeItem)
                        {
                            CommonChargeItem ncci = new CommonChargeItem();
                            ncci.AreaPrice = cci.AreaPrice;
                            ncci.CommonChargeItemId = cci.CommonChargeItemId;
                            ncci.ItemName = cci.ItemName;
                            //ncci.orderInfo = newOrder;
                            ncci.SinglePrice = cci.SinglePrice;
                            cci.UnitPrice = cci.UnitPrice;
                            newOrder.CommonChargeItem.Add(ncci);

                        }
                        //讲分配的数量产生冻结数量
                        if (!currOrder.Tb_ProductReference.IsLoaded)
                        {
                            currOrder.Tb_ProductReference.Load();
                        }
                        //写入操作
                        StoreOperRec sor = new StoreOperRec();
                        sor.OperTime = DateTime.Now;
                        sor.OperRemark = "";
                        sor.StoreOperid = UserID;
                        sor.Tb_Product = currOrder.Tb_Product;
                        sor.users = db.users.FirstOrDefault(u => u.userID == UserID);
                        sor.OperBeforeNum = currOrder.Tb_Product.StoreNum;
                        sor.OperBeforeLockNum = currOrder.Tb_Product.LockNum;
                        currOrder.Tb_Product.LockNum += CurrProductOrder.OperNum;
                        sor.OperAfterLockNum = currOrder.Tb_Product.LockNum;
                        sor.OperAfterNum = currOrder.Tb_Product.StoreNum;
                        currOrder.Tb_Product.StoreOperRec.Add(sor);
                        db.AddToStoreOperRec(sor);

                        db.SaveChanges();//创建新产品

                        //吧新产品的流程改到下一个节点
                        newOrder.OrderProductCode = "2" + string.Format("{0:d9}", Convert.ToInt32(newOrder.OrderProductId));
                        returnBool = _wflBll.OverCurTech(techID, UserID, newOrder, db);

                    }

                }
            }
            return returnBool;
        }
        
    }

}
